<!--
 * @Description: 应用导出 
 * @Author: 马山
 * @Date: 2020-03-09 15:36:26
 * @LastEditTime: 2020-03-19 12:56:05
 * @LastEditors: 马山
 -->
<template>
    <el-dialog title="应用导出" :visible="isShow" :before-close="closeDialog">
        <span>导出 《{{ title }}》</span>
        <span v-if="radio=='2'">-试用</span>
        <el-divider></el-divider>
        <div>请选择加密策略</div>
        <br>
        <div>
            <el-radio v-for="(item,index) in appList" :key="item.key" class="radioDom" v-model="radio" :label="item.id">
                {{ item.text }}    
            </el-radio>
            <!-- <el-radio class="radioDom" v-model="radio" label="0">私有包</el-radio>
            <el-radio class="radioDom" v-model="radio" label="1">通用试用包</el-radio> -->
        </div>
        <br>
        <!-- 私有包提示 -->
        <div v-if="radio=='private_app_0'">
            <span>说明:</span>
            <p>导出包仅限本单位导入使用。</p>
        </div>
        <!-- 通用试用包 -->
        <div v-if="radio=='custom_trail_1'">
            <span>说明:</span>
            <p>1、试用包可以安装到任何服务器！</p>
            <p>2、试用包导出试用的试用期为2个月，以导入时间为准！</p>
            <p>3、试用包内的表单可以随意导出</p>
        </div>
        <span slot="footer" class="dialog-footer">
            <el-button @click="openDownload">
                下一步
                <!-- <a class='download' :href='downloadhttp' download=""  title="下载">下一步</a> -->
            </el-button>
            <el-button @click="closeDialog">取 消</el-button>
        </span>
    </el-dialog>

    
</template>

<script>



export default {
    name: "download",
    props: { },
    components: {},
    computed: {},
    data: () => ({
        // 组件入口参数
        isShow:false,
        title:"",
        bizConfigId:"",
        targetCorpid:"",
        userInfo:{},

        radio:"private_app_0",//默认选中私有包
        appList:[]
    }),
    created() {},
    mounted() {},
    watch: {},
    methods: {
        //组件入口
        open(data){
            if(data){
                this.userInfo = data.userInfo;
                this.title = data.title;
                this.bizConfigId = data.bizConfigId;
                this.$nextTick(()=>{
                    this.getAppValidate();
                })
            }
        },
        closeDialog(){
            this.radio = "private_app_0";
            this.appList = [];
            this.isShow = false;
        },
        //通过应用Id查询导出授权类型列表
        getAppValidate(){
            let data = {
                bizConfigId:this.bizConfigId
            };
            this.api.getAppValidate(
                data
            ).then(res => {
                console.log(res);
                let data = res.data;
                if(data){
                    this.isShow = true;
                    let exportTypeList = data.exportTypeList;
                    for(let i=0,len=exportTypeList.length;i<len;i++){
                        let item = exportTypeList[i];
                        item["id"]=item.key+"_"+i;
                        this.appList.push(item);
                    }
                    //单独再添加一个选项
                    // this.appList.push({
                    //     id:"design_center_app_"+exportTypeList.length,
                    //     key:"design_center_app",
                    //     text:"云设计中心私有包"
                    // });
                }else{
                    this.$alert(data);
                }
            }).catch(err => {
                console.log(err);
                this.closeDialog();
                this.$alert(err.msg || err.message );
            });
        },
        //打开下载
        openDownload(){
            //下载包到客户环境使用
            let radioDom = this.radio.lastIndexOf("_");
            let radioLength = this.radio.length;
            let index = this.radio.substring(radioDom + 1, radioLength);//截
            let data = {
                spaceId:this.userInfo.spaceId,
                bizConfigId:this.bizConfigId,       //应用id
                appExportType:this.appList[index].key,    //导出类型
                permissionType:null  //授权类型
            }
            const loading = this.$loading({
                // lock: true,
                text: 'Loading',
                spinner: 'el-icon-loading',
                background: 'rgba(0, 0, 0, 0.7)'
            });
            this.api.download(
                data
            ).then(res => {
                loading.close();
                console.log(res);
                let data = res.data;
                this.closeDialog();
                if(!data.url){
                    this.$alert("导出失败,导出地址返回为空");
                }else{
                    window.open(data.url); //执行下载
                }
            }).catch(err => {
                loading.close();
                this.closeDialog();
                this.$alert(err.msg || err.message);
               console.warn(err.msg);
            });

        }
    }
};
</script>

<style lang="scss" scoped>

.radioDom{
    margin-bottom: 10px;
    display:block;
}

</style>